package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private  DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper ;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dto
     */
    @Override
    @Transactional // 开启事务(设计到多张表的增删改查需要开启事务）
    public void addDish(DishDTO dto) {
        //1.构造菜品基本信息，将其存入dish表中
        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dto,dish);
        //调用mapper保存方法
        dishMapper.insert(dish);


        log.info("dishId={}",dish.getId());

        //2.构造菜品对应的口味数据，并将其存入dish_flavor表中
        List<DishFlavor> dishFlavorList = dto.getFlavors();
        //关联菜品id
        dishFlavorList.forEach(flavor -> {
            flavor.setDishId(dish.getId());
            //或者：一条一条插入
            //dishFlavorMapper.insert(flavor);
        });
        //集合：批量插入
        dishFlavorMapper.insertBatch(dishFlavorList);
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult list(DishPageQueryDTO dto) {
        //1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2.调用mapper分页查询，并强转为page
        Page<DishVO> page = dishMapper.list(dto);
        //3.封装pageresult并返回
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //1.删除菜品之前，需要判断菜品是否启售，若启售则不允许删除
        ids.forEach(id -> {
            Dish dish =dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        //2.判断菜品是否关联了套餐，若关联则不允许删除
        Integer count =  setmealDishMapper.countByDishId(ids);
        if(count > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.删除菜品
        dishMapper.deleteBacth(ids);

        //4.删除菜品对应的口味数据
        dishFlavorMapper.deleteBatch(ids);

    }
    /**
     * 根据id查询菜品和对应的口味数据
     * @param id
     * @return
     */
    @Override
    public DishVO getByID(Long id) {
        //1.根据id查询菜品基本数据
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);

        //构造vo对象并返回
        dishVO.setFlavors(dishFlavorMapper.selectByDishId(id));
        return dishVO;
    }
}
